from collections import Counter

import pandas as pd


def load_and_process_data():
    # 读取CSV文件
    df = pd.read_csv('static/data/data_test.csv')

    # 1. 品牌投诉量TOP10统计
    brand_complaints = df['brand'].value_counts().head(10).to_dict()

    # 2. 问题类型分布
    problem_types = []
    for problems in df['problem'].str.split(','):
        if isinstance(problems, list):
            problem_types.extend(problems)
    problem_distribution = Counter(problem_types).most_common(10)

    # 3. 车型投诉量TOP10
    series_complaints = df['series'].value_counts().head(10).to_dict()

    # 4. 状态分布
    status_distribution = df['status'].value_counts().to_dict()

    # 5. 每日投诉量趋势
    daily_complaints = df['time'].value_counts().sort_index().to_dict()

    # 6. 新能源vs燃油车投诉对比
    new_energy_keywords = ['新能源', '电动', 'EV', '混动', 'PHEV', 'DM']
    df['is_new_energy'] = df['series'].apply(lambda x: any(keyword in x for keyword in new_energy_keywords))
    energy_type_complaints = df['is_new_energy'].value_counts().to_dict()

    return {
        'brand_complaints': brand_complaints,
        'problem_distribution': dict(problem_distribution),
        'series_complaints': series_complaints,
        'status_distribution': status_distribution,
        'daily_complaints': daily_complaints,
        'energy_type_complaints': energy_type_complaints
    }
